package middleware

import (
	"doctor-medicine-system/pkg"
	"net/http"
)

type UserAgentMiddleware struct {
}

func NewUserAgentMiddleware() *UserAgentMiddleware {
	return &UserAgentMiddleware{}
}

func (m *UserAgentMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO generate middleware implement function, delete after code implementation

		token := r.Header.Get("token")

		handler, err := pkg.GetToken(token)
		if err != nil {
			return
		}

		r.Header.Set("docId", handler["docId"].(string))

		// Passthrough to next handler if need
		next(w, r)
	}
}
